﻿


/*yui list page 
* 创建：江怀玉
* 日期: 2011.11.30
* 版本
*
*
*
*/

//DataTable
var mDataTable,
requireUrl="";
/*重新加载数据*/
function LoadData() {

    var requestStr = "pagesize=" + pagesize + "&pageindex=" + pageindex + "&entity=" + const_entityName + "&order=" + const_Order + "&r=" + Math.random() + "&zip=94089&results=10&output=json&functionid=" + const_functionId + "&customCondition=" + const_CustomCondition;
    //GetTotal("AjaxData/JsonList.aspx?onlyTotal=true&"+requestStr);
    mDataTable.requery(requestStr);
    //BindData();
}


function RefreshData() {
    initialRequestUrl = "pageindex=1&entity=" + const_entityName + "&order=" + const_Order + "&r=" + Math.random() + "&zip=94089&results=10&output=json&functionid=" + const_functionId + "&customCondition=" + decodeURI(const_CustomCondition);
    BindData();
    ReLoadData();
}

/*刷新时候调用*/
function ReLoadData() {
    var requestStr = "pagesize=" + pagesize + "&pageindex=" + pageindex + "&entity=" + const_entityName + "&order=" + const_Order + "&r=" + Math.random() + "&zip=94089&results=10&output=json&functionid=" + const_functionId + "&customCondition=" + const_CustomCondition;
    GetTotal("AjaxData/JsonList.aspx?onlyTotal=true&" + requestStr);
    mDataTable.requery(requestStr);
    //BindData();
}
/*关闭窗口*/
function closeWin() {
    //Y.util.Dom.get("yui-gen7-button").setAttribute("style", "display:block");
    YAHOO.example.container.dialog1.hide();
    LoadData();
}

/* 对应的方法和ID*/
var _fn, _id;
function ShowDialog(fn, id) {
    _fn = fn;
    _id = id;
    YAHOO.example.container.dialog2.show();
}

/*数据绑定*/
function BindData() {
    YAHOO.example.XHR_JSON = function () {
        /*定义数据*/
        var myColumnDefs = const_DataFields;
        /*定义数据请求*/
        if (requireUrl == "") {
            requireUrl = "AjaxData/JSONList.aspx?";
        }
        var myDataSource = new YAHOO.util.DataSource(requireUrl);
        myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        myDataSource.connXhrMode = "queueRequests";
        /*定义数据源的请求结构*/
        myDataSource.responseSchema = {
            resultsList: "rows",
            metaFields: { totalRecords: "total" },
            fields: const_DataColums
        };

        this.myDataTable = new YAHOO.widget.DataTable("json", myColumnDefs,
                    myDataSource,
                    { initialRequest: initialRequestUrl
                    , selectionMode: "single"
                    });

        mDataTable = this.myDataTable;
        var mySuccessHandler = function () {
            this.set("sortedBy", null);
            this.onDataReturnAppendRows.apply(this, arguments);
        };
        var myFailureHandler = function () {
            //this.showTableMessage(YAHOO.widget.DataTable.MSG_ERROR, YAHOO.widget.DataTable.CLASS_ERROR);
            this.showTableMessage("没有数据", "没有数据");
            this.onDataReturnAppendRows.apply(this, arguments);
        };

        // 定义回传方法
        var callbackObj = {
            success: mySuccessHandler,
            failure: myFailureHandler,
            scope: myDataTable
        };


        // 调用自定义双击行的方法
        var onRowDoubleClick = function () {
            if (typeof (CustomDbClick) == "function") {
                CustomDbClick();
            } else {
                if (typeof (fnEdit) == "function") {
                    fnEdit("e");
                }
            }
        };


        var onCellClick = function () {
            var ctrl = event.srcElement;
            fnCtrl(ctrl);
        }

        // 注册相关方法给相关事件
        this.myDataTable.subscribe("rowMouseoverEvent", this.myDataTable.onEventHighlightRow);
        this.myDataTable.subscribe("rowMouseoutEvent", this.myDataTable.onEventUnhighlightRow);
        this.myDataTable.subscribe("rowClickEvent", this.myDataTable.onEventSelectRow);
        this.myDataTable.subscribe("cellClickEvent", this.myDataTable.onEventShowCellEditor);
        this.myDataTable.subscribe("rowDblclickEvent", onRowDoubleClick);
        this.myDataTable.subscribe("cellClickEvent", onCellClick);
        this.myDataTable.subscribe("checkboxClickEvent", function (oArgs) {
            var elCheckbox = oArgs.target;
            var oRecord = this.getRecord(elCheckbox);
            oRecord.setData("iSun_Selected", elCheckbox.checked);
        });

        // Programmatically select the first row 
        this.myDataTable.selectRow(this.myDataTable.getTrEl(0));
        hideCol();
    } ();
}
//隐藏/栏位
function hideCol() { mDataTable.hideColumn(parseInt(0)); mDataTable.refreshView(); };
//
var _o;
function ontest(o) {
    _o.innerHTML = o.value;
    var f = (_o.parentNode.className.split(' ')[1].replace('yui-dt-col-', ''));
    setValue(f, o.value);
    _o = undefined;
}

///f=字段名 v=值
var temparr = [];
var setValue = function (f, v) {
    var recordID = mDataTable.getSelectedRows()[0];
    var record = mDataTable.getRecord(recordID);
    record._oData[f] = v;
    temparr.join(record._oData);
}

var fnCtrl=function(ctrl){
    if (ctrl.nodeName == "DIV") {
        if (ctrl != _o) {
            var o = document.getElementById("tx");
            if (o != undefined) {
                _o.innerHTML = document.getElementById("tx").value;
            }
        }
        _o = ctrl;
        var ctrl2 = "<input id='tx' width='100%' onblur='ontest(this)' value='" + ctrl.innerHTML + "'>";
        ctrl.innerHTML = ctrl2;
        var o = document.getElementById("tx");
        o.focus();
        //alert(ctrl.innerHTML);
    }
}

//  己删除列表
var deleteList = [];
//  编辑过的列表
var editlist = [];
//  新增的列表
var newlist = [];

var fnNew = function () {
    // Reset sort
    mDataTable.set("sortedBy", null);
    //alert("");
    index = mDataTable.getRecordSet()._records.length;
    var entity = const_BlankInstance;
    mDataTable.addRows(entity, index);
}
/* -----------------------------------------
*  定义对话窗口功能
*  
*
*
*--------------------------------------------*/
YAHOO.util.Event.addListener(window, "load", BindData);
YAHOO.namespace("example.container");
YAHOO.util.Event.onDOMReady(function () {

    // Define various event handlers for Dialog
    var handleSubmit = function () {
        var editwindow = document.getElementById("EditUIWindow");
        if (editwindow) {
            editwindow.contentWindow.Save();
        }
    };

    var handleCancel = function () {
        this.cancel();
    };
    var handleSubmit1 = function () {
        var editwindow = document.getElementById("EditUIWindow1");
        if (editwindow) {
            editwindow.contentWindow.ChooseInfo();
        }
        YAHOO.example.container.dialog2.hide();
    }
    var handleSuccess = function (o) {
        var response = o.responseText;
        response = response.split("<!")[0];
        document.getElementById("resp").innerHTML = response; fnSetField();
    };
    var handleFailure = function (o) {
        alert("Submission failed: " + o.status);
    };

    // Remove progressively enhanced content class, just before creating the module
    YAHOO.util.Dom.removeClass("dialog1", "yui-pe-content");

    /*
    数据编辑
    */
    YAHOO.example.container.dialog1 = new YAHOO.widget.Dialog("dialog1",
							    { width: const_EditWindowWidth,
							        height: const_EditWindowHeight,
							        fixedcenter: true,
							        visible: false,
							        draggable: true,
							        close: true,
							        autofillheight: "body",
							        constraintoviewport: true,
							        buttons: [{ text: "保存", handler: handleSubmit, isDefault: true },
								          { text: "取消", handler: handleCancel}]
							    });
    /*
    
    */
    YAHOO.example.container.dialog2 = new YAHOO.widget.Dialog("dialog2",
							    { width: "420px",
							        height: "400px",
							        fixedcenter: true,
							        visible: false,
							        draggable: true,
							        close: true,
							        autofillheight: "body",
							        constraintoviewport: true,
							        buttons: [{ text: "保存", handler: handleSubmit1, isDefault: true },
								          { text: "取消", handler: handleCancel}]
							    });

    /*
    
    */
    YAHOO.example.container.dialog3 = new YAHOO.widget.Dialog("dialog3",
							    { width: "350px",
							        height: "90px",
							        fixedcenter: true,
							        visible: false,
							        draggable: true,
							        close: true,
							        autofillheight: "body",
							        constraintoviewport: true,
							        buttons: [{ text: "确定", handler: DeleteData, isDefault: true },
								          { text: "取消", handler: handleCancel}]
							    });

    /*
    查询
    */
    YAHOO.example.container.dialog4 = new YAHOO.widget.Dialog("dialog4",
							    { width: "650px",
							        height: "350px",
							        fixedcenter: true,
							        visible: false,
							        draggable: true,
							        close: true,
							        autofillheight: "body",
							        constraintoviewport: true,
							        buttons: [
							    { text: "取消", handler: handleCancel}]
							    });
    YAHOO.example.container.dialog5 = new YAHOO.widget.Dialog("dialog5",
							    { width: const_EditWindowWidth,
							        height: const_EditWindowHeight,
							        fixedcenter: true,
							        visible: false,
							        draggable: true,
							        close: true,
							        autofillheight: "body",
							        constraintoviewport: true,
							        buttons: [
							    { text: "取消", handler: handleCancel}]
							    });
    // Validate the entries in the form to require that both first and last name are entered


    // Wire up the success and failure handlers
    YAHOO.example.container.dialog1.callback = { success: handleSuccess,
        failure: handleFailure
    };

    // Render the Dialog
    YAHOO.example.container.dialog1.render();
    YAHOO.example.container.dialog2.render();
    YAHOO.example.container.dialog3.render();
    YAHOO.example.container.dialog4.render();
    YAHOO.example.container.dialog5.render();

    /* 对话框
    ****/
    //----------------------------------------------------*/
    // YAHOO.util.Event.addListener("btnAddNew", "click", YAHOO.example.container.dialog1.show, YAHOO.example.container.dialog1, true);



    //拖动大小
    var resize = new YAHOO.util.Resize("dialog1", {
        handles: ["br"],
        autoRatio: false,
        minWidth: 450,
        minHeight: 400,
        status: false
    });
    resize.on("startResize", function (args) {

        if (this.cfg.getProperty("constraintoviewport")) {
            var D = YAHOO.util.Dom;

            var clientRegion = D.getClientRegion();
            var elRegion = D.getRegion(this.element);

            resize.set("maxWidth", clientRegion.right - elRegion.left - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
            resize.set("maxHeight", clientRegion.bottom - elRegion.top - YAHOO.widget.Overlay.VIEWPORT_OFFSET);
        } else {
            resize.set("maxWidth", null);
            resize.set("maxHeight", null);
        }

    }, YAHOO.example.container.dialog1, true);
    resize.on("resize", function (args) {
        var panelHeight = args.height;
        this.cfg.setProperty("height", panelHeight + "px");
    }, YAHOO.example.container.dialog1, true);
});


